//
//  CollectionViewController.m
//  JiongTu
//
//  Created by Dupengfei on 16/4/15.
//  Copyright © 2016年 YSKJ. All rights reserved.
//

#import "CollectionViewController.h"
#import "ControllerManger.h"
#import <AVUser.h>
#import <SVProgressHUD.h>
#import <AVQuery.h>
#import <AVCloud.h>
#import "OrderTableViewCell.h"
#import "JTDriverTrip.h"
#import "JTUser.h"
#import "OrderDetailedViewController.h"
@interface CollectionViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIButton *findCar;
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong) NSArray * collection;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (nonatomic, strong)NSMutableArray * array;
@property (nonatomic, strong)JTDriverTrip * driverTrip;
@end

@implementation CollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initializeDataSouce];
    
    [self initializeInterface];
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    [SVProgressHUD dismiss];

}

-(void)initializeInterface
{
  self.title =@"我的收藏";
    
}

-(void)initializeDataSouce
{
    
    [SVProgressHUD showWithStatus:@"加载中"];
    
    _array =[NSMutableArray array];
    _collection =[NSArray array];
    NSDictionary * dic = @{@"objectId":[AVUser currentUser].objectId};
    [AVCloud callFunctionInBackground:@"collection" withParameters:dic block:^(id object, NSError *error) {
        _collection =object;
        
        if (object)
        {
        for (int i=0; i<_collection.count; i++) {

            [AVCloud callFunctionInBackground:@"driverTrip" withParameters:@{@"objectId":_collection[i]} block:^(id objects, NSError *error)
          {
              NSArray * ar = objects;
              
              if (ar.count>0) {
                  [_array addObject:[JTDriverTrip tripWithAVObjects:objects]];
                  
                  [SVProgressHUD showSuccessWithStatus:@"刷新成功！"];
                  
                  [self.view addSubview:self.tableView];
                  
                  [_image removeFromSuperview];
                  
                  [_findCar removeFromSuperview];
                  
                  [_label removeFromSuperview];
                  
                   [_tableView reloadData];
              }
              else
              {
                  [SVProgressHUD dismiss];
              }
              
              

          }];
        }
        }
        else
        {
            [SVProgressHUD dismiss];
        }
        }];
    }


#pragma mark-delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _collection.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderTableViewCell*cell = [[[NSBundle mainBundle] loadNibNamed:@"OrderTableViewCell" owner:nil options:nil] lastObject];
    
    
    if (_collection.count ==_array.count && _collection.count>0) {
        
        JTDriverTrip * driverTrip = _array[indexPath.row][0];
        cell.oneDriverTrip = driverTrip;
    
    }
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderDetailedViewController * view =[[OrderDetailedViewController alloc]init];
    
    view.driverTrip =_array[indexPath.row][0];
    
    NSLog(@"%@",_driverTrip);
    
    [self.navigationController pushViewController:view animated:YES];
   

}

#pragma mark-交互
- (IBAction)findCarPressed:(UIButton *)sender {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}


#pragma mark- getter

-(UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView =[[UITableView alloc]initWithFrame:self.view.bounds];
        
        _tableView.backgroundColor = [UIColor colorWithRed:241/255.0 green:241/255.0 blue:241/255.0 alpha:1];
        
        _tableView.delegate =self;
        
        _tableView.dataSource =self;
        
         self.tableView.rowHeight =95;
    }
    
    return _tableView;
}

@end
